//
//  DDurationPickerView.swift
//  Drop
//
//  Created by hushaohua on 2018/2/7.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class DDurationPickerView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        self.addSubview(self.picker);
        self.addSubview(self.endButton);
        self.addSubview(self.beginButton);
        self.addSubview(self.doneButton);
        
        let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(tapTimePicker(_:)))
        self.addGestureRecognizer(tap);
    }
    
    var tapClosure:(() -> Void)?
    @objc func tapTimePicker(_ sender:UIGestureRecognizer) -> Void{
        if (self.tapClosure != nil){
            self.tapClosure!();
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var picker:UIDatePicker = {
        let picker:UIDatePicker = UIDatePicker(frame: CGRect(x:0,y:192,width:self.bounds.width,height:240));
        picker.backgroundColor = UIColor.white;
        picker.datePickerMode = .date;
        picker.minuteInterval = 5;
        picker.addTarget(self, action: #selector(datePickerChanged(_:)), for: .valueChanged);
        return picker;
    }();
    
    @objc func datePickerChanged(_ sender:UIDatePicker) -> Void{
        if (self.beginButton.isSelected){
            self.beginDate = picker.date;
            self.resetBeginDate();
        }else{
            self.endDate = picker.date;
            self.resetEndDate();
        }
    }
    func resetEndDate() -> Void{
        self.endButton.setTitle("至\(self.timeStringFrom(self.endDate!))", for: .normal);
    }
    func resetBeginDate() -> Void{
        self.beginButton.setTitle("从\(self.timeStringFrom(self.beginDate!))", for: .normal);
    }
    
    var beginDate:Date?{
        didSet{
            self.resetBeginDate();
        }
    }
    var beginClosure:(() -> Void)?
    @objc func beginButtonClicked(_ sender:UIButton) -> Void{
        self.beginButton.isSelected = true;
        self.endButton.isSelected = false;
        self.picker.date = self.beginDate!;
        if (self.beginClosure != nil){
            self.beginClosure!();
        }
    }
    
    func timeStringFrom(_ date:Date) -> String {
        let dateFormatter:DateFormatter = DateFormatter();
        dateFormatter.dateFormat = "HH:mm";
        return dateFormatter.string(from: date);
    }
    
    var endDate:Date?{
        didSet{
            self.resetEndDate();
        }
    }
    var endClosure:(() -> Void)?
    @objc func endButtonClicked(_ sender:UIButton) -> Void{
        self.beginButton.isSelected = false;
        self.endButton.isSelected = true;
        self.picker.date = self.endDate!;
        if (self.endClosure != nil){
            self.endClosure!();
        }
    }
    
    lazy var beginButton:UIButton = {
        let button:UIButton = self.undisturbedTimeButton("从", 0);
        button.isSelected = true;
        button.addTarget(self, action: #selector(beginButtonClicked(_:)), for: .touchUpInside);
        return button;
    }();
    
    lazy var endButton:UIButton = {
        let button:UIButton = self.undisturbedTimeButton("至", self.bounds.width / 2);
        button.addTarget(self, action: #selector(endButtonClicked(_:)), for: .touchUpInside);
        return button;
    }();
    
    func undisturbedTimeButton(_ title:String, _ x:CGFloat) -> UIButton{
        let button:UIButton = UIButton(frame:CGRect(x:x,y:160,width:self.bounds.width / 2,height:40));
        button.setTitle(title, for: .normal);
        if (x == 0){
            let layer:CALayer = CALayer();
            layer.backgroundColor = UIColor.red.cgColor;
            layer.frame = CGRect(x:self.bounds.width / 2,y:14,width:1,height:12);
            button.layer.addSublayer(layer);
        }
        button.setTitleColor(UIColor.lightGray, for: .normal);
        button.setTitleColor(UIColor.h_blue(), for: .selected);
        button.backgroundColor = UIColor.white;
        return button;
    }
    
    lazy var doneButton:UIButton = {
        var button:UIButton = UIButton(frame:CGRect(x:0,y:432,width:self.bounds.width,height:44));
        button.setTitle("确定", for: .normal);
        button.setTitleColor(UIColor.lightGray, for: .normal);
        button.backgroundColor = UIColor.white;
        button.addTarget(self, action: #selector(doneButtonClicked(_:)), for: .touchUpInside);
        return button;
    }();
    var doneClosure:(() -> Void)?
    @objc func doneButtonClicked(_ sender:UIButton) -> Void{
        if (self.doneClosure != nil){
            self.doneClosure!();
        }
    }

}
